home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gcc_260.zip / gcc_260 / cp / except.c < prev    next >
C/C++ Source or Header  |  1994-07-06  |  40KB  |  1,482 lines

  1. /* Handle exceptional things in C++.
  2.    Copyright (C) 1989, 1992, 1993, 1994 Free Software Foundation, Inc.
  3.    Contributed by Michael Tiemann <tiemann@cygnus.com>
  4.    Rewritten by Mike Stump <mrs@cygnus.com>, based upon an
  5.    initial re-implementation courtesy Tad Hunt.
  6.  
  7. This file is part of GNU CC.
  8.  
  9. GNU CC is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2, or (at your option)
  12. any later version.
  13.  
  14. GNU CC is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. GNU General Public License for more details.
  18.  
  19. You should have received a copy of the GNU General Public License
  20. along with GNU CC; see the file COPYING.  If not, write to
  21. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  22.  
  23.  
  24. /* High-level class interface. */
  25.  
  26. #include "config.h"
  27. #include "tree.h"
  28. #include "rtl.h"
  29. #include "cp-tree.h"
  30. #include "flags.h"
  31. #include "obstack.h"
  32. #include "expr.h"
  33.  
  34. extern void (*interim_eh_hook)    PROTO((tree));
  35.  
  36. /* holds the fndecl for __builtin_return_address () */
  37. tree builtin_return_address_fndecl;
  38.  
  39. /* Define at your own risk!  */
  40. #ifndef CROSS_COMPILE
  41. #ifdef sun
  42. #ifdef sparc
  43. #define TRY_NEW_EH
  44. #endif
  45. #endif
  46. #endif
  47.  
  48. #ifndef TRY_NEW_EH
  49.  
  50. static void
  51. sorry_no_eh ()
  52. {
  53.   static int warned = 0;
  54.   if (! warned)
  55.     {
  56.       sorry ("exception handling not supported");
  57.       warned = 1;
  58.     }
  59. }
  60.  
  61. void
  62. build_exception_table ()
  63. {
  64. }
  65.  
  66. void
  67. expand_exception_blocks ()
  68. {
  69. }
  70.  
  71. void
  72. start_protect ()
  73. {
  74. }
  75.  
  76. void
  77. end_protect (finalization)
  78.      tree finalization;
  79. {
  80. }
  81.  
  82. void
  83. expand_start_try_stmts ()
  84. {
  85.   sorry_no_eh ();
  86. }
  87.  
  88. void
  89. expand_end_try_stmts ()
  90. {
  91. }
  92.  
  93. void
  94. expand_start_all_catch ()
  95. {
  96. }
  97.  
  98. void
  99. expand_end_all_catch ()
  100. {
  101. }
  102.  
  103. void
  104. expand_start_catch_block (declspecs, declarator)
  105.      tree declspecs, declarator;
  106. {
  107. }
  108.  
  109. void
  110. expand_end_catch_block ()
  111. {
  112. }
  113.  
  114. void
  115. init_exception_processing ()
  116. {
  117. }
  118.  
  119. void
  120. expand_throw (exp)
  121.      tree exp;
  122. {
  123.   sorry_no_eh ();
  124. }
  125.  
  126. #else
  127.  
  128. static int
  129. doing_eh (do_warn)
  130.      int do_warn;
  131. {
  132.   if (! flag_handle_exceptions)
  133.     {
  134.       static int warned = 0;
  135.       if (! warned && do_warn)
  136.     {
  137.       error ("exception handling disabled, use -fhandle-exceptions to enable.");
  138.       warned = 1;
  139.     }
  140.       return 0;
  141.     }
  142.   return 1;
  143. }
  144.  
  145.  
  146. /*
  147. NO GNEWS IS GOOD GNEWS WITH GARRY GNUS: This version is much closer
  148. to supporting exception handling as per Stroustrup's 2nd edition.
  149. It is a complete rewrite of all the EH stuff that was here before
  150.     Shortcomings:
  151.         1. The type of the throw and catch must still match
  152.            exactly (no support yet for matching base classes)
  153.         2. Throw specifications of functions still doesnt't work.
  154.     Cool Things:
  155.         1. Destructors are called properly :-)
  156.         2. No overhead for the non-exception thrown case.
  157.         3. Fixing shortcomings 1 and 2 is simple.
  158.             -Tad Hunt    (tad@mail.csh.rit.edu)
  159.  
  160. */
  161.  
  162. /* A couple of backend routines from m88k.c */
  163.  
  164. /* used to cache a call to __builtin_return_address () */
  165. static tree BuiltinReturnAddress;
  166.  
  167.  
  168.  
  169.  
  170.  
  171. #include <stdio.h>
  172.  
  173. /* XXX - Tad: for EH */
  174. /* output an exception table entry */
  175.  
  176. static void
  177. output_exception_table_entry (file, start_label, end_label, eh_label)
  178.      FILE *file;
  179.      rtx start_label, end_label, eh_label;
  180. {
  181.   char label[100];
  182.  
  183.   fprintf (file, "\t%s\t ", ASM_LONG);    
  184.   if (GET_CODE (start_label) == CODE_LABEL)
  185.     {
  186.       ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (start_label));
  187.       assemble_name (file, label);
  188.     }
  189.   else if (GET_CODE (start_label) == SYMBOL_REF)
  190.     {
  191.       fprintf (stderr, "YYYYYYYYYEEEEEEEESSSSSSSSSSSS!!!!!!!!!!\n");
  192.       assemble_name (file, XSTR (start_label, 0));
  193.     }
  194.   putc ('\n', file);
  195.  
  196.   fprintf (file, "\t%s\t ", ASM_LONG);
  197.   ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (end_label));
  198.   assemble_name (file, label);
  199.   putc ('\n', file);
  200.  
  201.   fprintf (file, "\t%s\t ", ASM_LONG);
  202.   ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (eh_label));
  203.   assemble_name (file, label);
  204.   putc ('\n', file);
  205.  
  206.   putc ('\n', file);        /* blank line */
  207. }
  208.    
  209. static void
  210. easy_expand_asm (str)
  211.      char *str;
  212. {
  213.   expand_asm (build_string (strlen (str)+1, str));
  214. }
  215.  
  216. /* unwind the stack. */
  217. static void
  218. do_unwind (throw_label)
  219.      rtx throw_label;
  220. {
  221. #ifdef sparc
  222.   extern FILE *asm_out_file;
  223.   tree fcall;
  224.   tree params;
  225.   rtx return_val_rtx;
  226.  
  227.   /* call to  __builtin_return_address () */
  228.   params=tree_cons (NULL_TREE, integer_zero_node, NULL_TREE);
  229.   fcall = build_function_call (BuiltinReturnAddress, params);
  230.   return_val_rtx = expand_expr (fcall, NULL_RTX, SImode, 0);
  231.   /* In the return, the new pc is pc+8, as the value comming in is
  232.      really the address of the call insn, not the next insn.  */
  233.   emit_move_insn (return_val_rtx, plus_constant(gen_rtx (LABEL_REF,
  234.                              Pmode,
  235.                              throw_label), -8));
  236.   /* We use three values, PC, type, and value */
  237.   easy_expand_asm ("st %l0,[%fp]");
  238.   easy_expand_asm ("st %l1,[%fp+4]");
  239.   easy_expand_asm ("st %l2,[%fp+8]");
  240.   easy_expand_asm ("ret");
  241.   easy_expand_asm ("restore");
  242.   emit_barrier ();
  243. #endif
  244. #if m88k
  245.   rtx temp_frame = frame_pointer_rtx;
  246.  
  247.   temp_frame = memory_address (Pmode, temp_frame);
  248.   temp_frame = copy_to_reg (gen_rtx (MEM, Pmode, temp_frame));
  249.  
  250.   /* hopefully this will successfully pop the frame! */
  251.   emit_move_insn (frame_pointer_rtx, temp_frame);
  252.   emit_move_insn (stack_pointer_rtx, frame_pointer_rtx);
  253.   emit_move_insn (arg_pointer_rtx, frame_pointer_rtx);
  254.   emit_insn (gen_add2_insn (stack_pointer_rtx, gen_rtx (CONST_INT, VOIDmode,
  255.                              (HOST_WIDE_INT)m88k_debugger_offset (stack_pointer_rtx, 0))));
  256.  
  257. #if 0
  258.   emit_insn (gen_add2_insn (arg_pointer_rtx, gen_rtx (CONST_INT, VOIDmode,
  259.                            -(HOST_WIDE_INT)m88k_debugger_offset (arg_pointer_rtx, 0))));
  260.  
  261.   emit_move_insn (stack_pointer_rtx, arg_pointer_rtx);
  262.  
  263.   emit_insn (gen_add2_insn (stack_pointer_rtx, gen_rtx (CONST_INT, VOIDmode,
  264.                              (HOST_WIDE_INT)m88k_debugger_offset (arg_pointer_rtx, 0))));
  265. #endif
  266. #endif
  267. }
  268.  
  269.  
  270.  
  271. #if 0
  272. /* This is the startup, and finish stuff per exception table. */
  273.  
  274. /* XXX - Tad: exception handling section */
  275. #ifndef EXCEPT_SECTION_ASM_OP
  276. #define EXCEPT_SECTION_ASM_OP    "section\t.gcc_except_table,\"a\",@progbits"
  277. #endif
  278.  
  279. #ifdef EXCEPT_SECTION_ASM_OP
  280. typedef struct {
  281.     void *start_protect;
  282.     void *end_protect;
  283.     void *exception_handler;
  284.  } exception_table;
  285. #endif /* EXCEPT_SECTION_ASM_OP */
  286.  
  287. #ifdef EXCEPT_SECTION_ASM_OP
  288.  
  289.  /* on machines which support it, the exception table lives in another section,
  290.     but it needs a label so we can reference it...  This sets up that
  291.     label! */
  292. asm (EXCEPT_SECTION_ASM_OP);
  293. exception_table __EXCEPTION_TABLE__[1] = { (void*)0, (void*)0, (void*)0 };
  294. asm (TEXT_SECTION_ASM_OP);
  295.  
  296. #endif /* EXCEPT_SECTION_ASM_OP */
  297.  
  298. #ifdef EXCEPT_SECTION_ASM_OP
  299.  
  300.  /* we need to know where the end of the exception table is... so this
  301.     is how we do it! */
  302.  
  303. asm (EXCEPT_SECTION_ASM_OP);
  304. exception_table __EXCEPTION_END__[1] = { (void*)-1, (void*)-1, (void*)-1 };
  305. asm (TEXT_SECTION_ASM_OP);
  306.  
  307. #endif /* EXCEPT_SECTION_ASM_OP */
  308.  
  309. #endif
  310.  
  311. void
  312. exception_section ()
  313. {
  314. #ifdef ASM_OUTPUT_SECTION_NAME
  315.   named_section (".gcc_except_table");
  316. #else
  317.   text_section ();
  318. #endif
  319. }
  320.  
  321.  
  322.  
  323.  
  324. /* from: my-cp-except.c */
  325.  
  326. /* VI: ":set ts=4" */
  327. #if 0
  328. #include <stdio.h> */
  329. #include "config.h"
  330. #include "tree.h"
  331. #include "rtl.h"
  332. #include "cp-tree.h"
  333. #endif
  334. #include "decl.h"
  335. #if 0
  336. #include "flags.h"
  337. #endif
  338. #include "insn-flags.h"
  339. #include "obstack.h"
  340. #if 0
  341. #include "expr.h"
  342. #endif
  343.  
  344. /* ======================================================================
  345.    Briefly the algorithm works like this:
  346.  
  347.      When a constructor or start of a try block is encountered,
  348.      push_eh_entry (&eh_stack) is called.  Push_eh_entry () creates a
  349.      new entry in the unwind protection stack and returns a label to
  350.      output to start the protection for that block.
  351.  
  352.      When a destructor or end try block is encountered, pop_eh_entry
  353.      (&eh_stack) is called.  Pop_eh_entry () returns the ehEntry it
  354.      created when push_eh_entry () was called.  The ehEntry structure
  355.      contains three things at this point.  The start protect label,
  356.      the end protect label, and the exception handler label.  The end
  357.      protect label should be output before the call to the destructor
  358.      (if any). If it was a destructor, then its parse tree is stored
  359.      in the finalization variable in the ehEntry structure.  Otherwise
  360.      the finalization variable is set to NULL to reflect the fact that
  361.      is the the end of a try block.  Next, this modified ehEntry node
  362.      is enqueued in the finalizations queue by calling
  363.      enqueue_eh_entry (&queue,entry).
  364.  
  365.     +---------------------------------------------------------------+
  366.     |XXX: Will need modification to deal with partially        |
  367.     |            constructed arrays of objects        |
  368.     |                                |
  369.     |    Basically, this consists of keeping track of how many    |
  370.     |    of the objects have been constructed already (this    |
  371.     |    should be in a register though, so that shouldn't be a    |
  372.     |    problem.                        |
  373.     +---------------------------------------------------------------+
  374.  
  375.      When a catch block is encountered, there is a lot of work to be
  376.      done.
  377.  
  378.      Since we don't want to generate the catch block inline with the
  379.      regular flow of the function, we need to have some way of doing
  380.      so.  Luckily, we have a couple of routines "get_last_insn ()" and
  381.      "set_last_insn ()" provided.  When the start of a catch block is
  382.      encountered, we save a pointer to the last insn generated.  After
  383.      the catch block is generated, we save a pointer to the first
  384.      catch block insn and the last catch block insn with the routines
  385.      "NEXT_INSN ()" and "get_last_insn ()".  We then set the last insn
  386.      to be the last insn generated before the catch block, and set the
  387.      NEXT_INSN (last_insn) to zero.
  388.  
  389.      Since catch blocks might be nested inside other catch blocks, and
  390.      we munge the chain of generated insns after the catch block is
  391.      generated, we need to store the pointers to the last insn
  392.      generated in a stack, so that when the end of a catch block is
  393.      encountered, the last insn before the current catch block can be
  394.      popped and set to be the last insn, and the first and last insns
  395.      of the catch block just generated can be enqueue'd for output at
  396.      a later time.
  397.           
  398.      Next we must insure that when the catch block is executed, all
  399.      finalizations for the matching try block have been completed.  If
  400.      any of those finalizations throw an exception, we must call
  401.      terminate according to the ARM (section r.15.6.1).  What this
  402.      means is that we need to dequeue and emit finalizations for each
  403.      entry in the ehQueue until we get to an entry with a NULL
  404.      finalization field.  For any of the finalization entries, if it
  405.      is not a call to terminate (), we must protect it by giving it
  406.      another start label, end label, and exception handler label,
  407.      setting its finalization tree to be a call to terminate (), and
  408.      enqueue'ing this new ehEntry to be output at an outer level.
  409.      Finally, after all that is done, we can get around to outputting
  410.      the catch block which basically wraps all the "catch (...) {...}"
  411.      statements in a big if/then/else construct that matches the
  412.      correct block to call.
  413.      
  414.      ===================================================================== */
  415.  
  416. extern rtx emit_insn        PROTO((rtx));
  417. extern rtx gen_nop        PROTO(());
  418.  
  419. /* local globals for function calls
  420.    ====================================================================== */
  421.  
  422. /* used to cache "terminate ()", "unexpected ()", "set_terminate ()", and
  423.    "set_unexpected ()" after default_conversion. (lib-except.c) */
  424. static tree Terminate, Unexpected, SetTerminate, SetUnexpected, CatchMatch;
  425.  
  426. /* used to cache __find_first_exception_table_match ()
  427.    for throw (lib-except.c)  */
  428. static tree FirstExceptionMatch;
  429.  
  430. /* used to cache a call to __unwind_function () (lib-except.c) */
  431. static tree Unwind;
  432.  
  433. /* holds a ready to emit call to "terminate ()". */
  434. static tree TerminateFunctionCall;
  435.  
  436. /* ====================================================================== */
  437.  
  438.  
  439.  
  440. /* data structures for my various quick and dirty stacks and queues
  441.    Eventually, most of this should go away, because I think it can be
  442.    integrated with stuff already built into the compiler. */
  443.  
  444. /* =================================================================== */
  445.  
  446. struct labelNode {
  447.     rtx label;
  448.     struct labelNode *chain;
  449.  };
  450.  
  451.  
  452. /* this is the most important structure here.  Basically this is how I store
  453.    an exception table entry internally. */
  454. struct ehEntry {
  455.     rtx start_label;
  456.     rtx end_label;
  457.     rtx exception_handler_label;
  458.  
  459.     tree finalization;
  460.  };
  461.  
  462. struct ehNode {
  463.     struct ehEntry *entry;
  464.     struct ehNode *chain;
  465.  };
  466.  
  467. struct ehStack {
  468.     struct ehNode *top;
  469.  };
  470.  
  471. struct ehQueue {
  472.     struct ehNode *head;
  473.     struct ehNode *tail;
  474.  };
  475.  
  476. struct exceptNode {
  477.     rtx catchstart;
  478.     rtx catchend;
  479.  
  480.     struct exceptNode *chain;
  481.  };
  482.  
  483. struct exceptStack {
  484.     struct exceptNode *top;
  485.  };
  486. /* ========================================================================= */
  487.  
  488.  
  489.  
  490. /* local globals - these local globals are for storing data necessary for
  491.    generating the exception table and code in the correct order.
  492.  
  493.    ========================================================================= */
  494.  
  495. /* Holds the pc for doing "throw" */
  496. rtx saved_pc;
  497. /* Holds the type of the thing being thrown. */
  498. rtx saved_throw_type;
  499. /* Holds the value being thrown.  */
  500. rtx saved_throw_value;
  501.  
  502. rtx throw_label;
  503.  
  504. static struct ehStack ehstack;
  505. static struct ehQueue ehqueue;
  506. static struct ehQueue eh_table_output_queue;
  507. static struct exceptStack exceptstack;
  508. static struct labelNode *false_label_stack = NULL;
  509. static struct labelNode *caught_return_label_stack = NULL;
  510. /* ========================================================================= */
  511.  
  512. /* function prototypes */
  513. static struct ehEntry *pop_eh_entry    PROTO((struct ehStack *stack));
  514. static void enqueue_eh_entry        PROTO((struct ehQueue *queue, struct ehEntry *entry));
  515. static void push_except_stmts        PROTO((struct exceptStack *exceptstack,
  516.                      rtx catchstart, rtx catchend));
  517. static int pop_except_stmts        PROTO((struct exceptStack *exceptstack,
  518.                      rtx *catchstart, rtx *catchend));
  519. static rtx push_eh_entry        PROTO((struct ehStack *stack));
  520. static struct ehEntry *dequeue_eh_entry    PROTO((struct ehQueue *queue));
  521. static void new_eh_queue        PROTO((struct ehQueue *queue));
  522. static void new_eh_stack        PROTO((struct ehStack *stack));
  523. static void new_except_stack        PROTO((struct exceptStack *queue));
  524. static void push_last_insn        PROTO(());
  525. static rtx pop_last_insn        PROTO(());
  526. static void push_label_entry        PROTO((struct labelNode **labelstack, rtx label));
  527. static rtx pop_label_entry        PROTO((struct labelNode **labelstack));
  528. static rtx top_label_entry        PROTO((struct labelNode **labelstack));
  529. static struct ehEntry *copy_eh_entry    PROTO((struct ehEntry *entry));
  530.  
  531.  
  532.  
  533. /* All my cheesy stack/queue/misc data structure handling routines
  534.  
  535.    ========================================================================= */
  536.  
  537. static void
  538. push_label_entry (labelstack, label)
  539.      struct labelNode **labelstack;
  540.      rtx label;
  541. {
  542.   struct labelNode *newnode=(struct labelNode*)xmalloc (sizeof (struct labelNode));
  543.  
  544.   newnode->label = label;
  545.   newnode->chain = *labelstack;
  546.   *labelstack = newnode;
  547. }
  548.  
  549. static rtx
  550. pop_label_entry (labelstack)
  551.      struct labelNode **labelstack;
  552. {
  553.   rtx label;
  554.   struct labelNode *tempnode;
  555.  
  556.   if (! *labelstack) return NULL_RTX;
  557.  
  558.   tempnode = *labelstack;
  559.   label = tempnode->label;
  560.   *labelstack = (*labelstack)->chain;
  561.   free (tempnode);
  562.  
  563.   return label;
  564. }
  565.  
  566. static rtx
  567. top_label_entry (labelstack)
  568.      struct labelNode **labelstack;
  569. {
  570.   if (! *labelstack) return NULL_RTX;
  571.  
  572.   return (*labelstack)->label;
  573. }
  574.  
  575. static void
  576. push_except_stmts (exceptstack, catchstart, catchend)
  577.      struct exceptStack *exceptstack;
  578.      rtx catchstart, catchend;
  579. {
  580.   struct exceptNode *newnode = (struct exceptNode*)
  581.     xmalloc (sizeof (struct exceptNode));
  582.  
  583.   newnode->catchstart = catchstart;
  584.   newnode->catchend = catchend;
  585.   newnode->chain = exceptstack->top;
  586.  
  587.   exceptstack->top = newnode;
  588. }
  589.  
  590. static int
  591. pop_except_stmts (exceptstack, catchstart, catchend)
  592.      struct exceptStack *exceptstack;
  593.      rtx *catchstart, *catchend;
  594. {
  595.   struct exceptNode *tempnode;
  596.  
  597.   if (!exceptstack->top) {
  598.     *catchstart = *catchend = NULL_RTX;
  599.     return 0;
  600.   }
  601.  
  602.   tempnode = exceptstack->top;
  603.   exceptstack->top = exceptstack->top->chain;
  604.  
  605.   *catchstart = tempnode->catchstart;
  606.   *catchend = tempnode->catchend;
  607.   free (tempnode);
  608.  
  609.   return 1;
  610. }
  611.  
  612. /* Push to permanent obstack for rtl generation.
  613.    One level only!  */
  614. static struct obstack *saved_rtl_obstack;
  615. void
  616. push_rtl_perm ()
  617. {
  618.   extern struct obstack permanent_obstack;
  619.   extern struct obstack *rtl_obstack;
  620.   
  621.   saved_rtl_obstack = rtl_obstack;
  622.   rtl_obstack = &permanent_obstack;
  623. }
  624.  
  625. /* Pop back to normal rtl handling.  */
  626. static void
  627. pop_rtl_from_perm ()
  628. {
  629.   extern struct obstack permanent_obstack;
  630.   extern struct obstack *rtl_obstack;
  631.   
  632.   rtl_obstack = saved_rtl_obstack;
  633. }
  634.  
  635. static rtx
  636. push_eh_entry (stack)
  637.      struct ehStack *stack;
  638. {
  639.   struct ehNode *node = (struct ehNode*)xmalloc (sizeof (struct ehNode));
  640.   struct ehEntry *entry = (struct ehEntry*)xmalloc (sizeof (struct ehEntry));
  641.  
  642.   if (stack == NULL) {
  643.     free (node);
  644.     free (entry);
  645.     return NULL_RTX;
  646.   }
  647.  
  648.   /* These are saved for the exception table.  */
  649.   push_rtl_perm ();
  650.   entry->start_label = gen_label_rtx ();
  651.   entry->end_label = gen_label_rtx ();
  652.   entry->exception_handler_label = gen_label_rtx ();
  653.   pop_rtl_from_perm ();
  654.  
  655.   entry->finalization = NULL_TREE;
  656.  
  657.   node->entry = entry;
  658.   node->chain = stack->top;
  659.   stack->top = node;
  660.  
  661.   enqueue_eh_entry (&eh_table_output_queue, copy_eh_entry (entry));
  662.  
  663.   return entry->start_label;
  664. }
  665.  
  666. static struct ehEntry *
  667. pop_eh_entry (stack)
  668.      struct ehStack *stack;
  669. {
  670.   struct ehNode *tempnode;
  671.   struct ehEntry *tempentry;
  672.  
  673.   if (stack && (tempnode = stack->top)) {
  674.     tempentry = tempnode->entry;
  675.     stack->top = stack->top->chain;
  676.     free (tempnode);
  677.  
  678.     return tempentry;
  679.   }
  680.  
  681.   return NULL;
  682. }
  683.  
  684. static struct ehEntry *
  685. copy_eh_entry (entry)
  686.      struct ehEntry *entry;
  687. {
  688.   struct ehEntry *newentry;
  689.  
  690.   newentry = (struct ehEntry*)xmalloc (sizeof (struct ehEntry));
  691.   memcpy ((void*)newentry, (void*)entry, sizeof (struct ehEntry));
  692.  
  693.   return newentry;
  694. }
  695.  
  696. static void
  697. enqueue_eh_entry (queue, entry)
  698.      struct ehQueue *queue;
  699.      struct ehEntry *entry;
  700. {
  701.   struct ehNode *node = (struct ehNode*)xmalloc (sizeof (struct ehNode));
  702.  
  703.   node->entry = entry;
  704.   node->chain = NULL;
  705.  
  706.   if (queue->head == NULL)
  707.     {
  708.       queue->head = node;
  709.     }
  710.   else
  711.     {
  712.       queue->tail->chain = node;
  713.     }
  714.   queue->tail = node;
  715. }
  716.  
  717. static struct ehEntry *
  718. dequeue_eh_entry (queue)
  719.      struct ehQueue *queue;
  720. {
  721.   struct ehNode *tempnode;
  722.   struct ehEntry *tempentry;
  723.  
  724.   if (queue->head == NULL)
  725.     return NULL;
  726.  
  727.   tempnode = queue->head;
  728.   queue->head = queue->head->chain;
  729.  
  730.   tempentry = tempnode->entry;
  731.   free (tempnode);
  732.  
  733.   return tempentry;
  734. }
  735.  
  736. static void
  737. new_eh_queue (queue)
  738.      struct ehQueue *queue;
  739. {
  740.   queue->head = queue->tail = NULL;
  741. }
  742.  
  743. static void
  744. new_eh_stack (stack)
  745.      struct ehStack *stack;
  746. {
  747.   stack->top = NULL;
  748. }
  749.  
  750. static void
  751. new_except_stack (stack)
  752.      struct exceptStack *stack;
  753. {
  754.   stack->top = NULL;
  755. }
  756. /* ========================================================================= */
  757.  
  758. void
  759. lang_interim_eh (finalization)
  760.      tree finalization;
  761. {
  762.   if (finalization)
  763.     end_protect (finalization);
  764.   else
  765.     start_protect ();
  766. }
  767.  
  768. /* sets up all the global eh stuff that needs to be initialized at the
  769.    start of compilation.
  770.  
  771.    This includes:
  772.         - Setting up all the function call trees
  773.         - Initializing the ehqueue
  774.         - Initializing the eh_table_output_queue
  775.         - Initializing the ehstack
  776.         - Initializing the exceptstack
  777. */
  778.  
  779. void
  780. init_exception_processing ()
  781. {
  782.   extern tree define_function ();
  783.   tree unexpected_fndecl, terminate_fndecl;
  784.   tree set_unexpected_fndecl, set_terminate_fndecl;
  785.   tree catch_match_fndecl;
  786.   tree find_first_exception_match_fndecl;
  787.   tree unwind_fndecl;
  788.   tree temp, PFV;
  789.  
  790.   interim_eh_hook = lang_interim_eh;
  791.  
  792.   /* void (*)() */
  793.   PFV = build_pointer_type (build_function_type (void_type_node, void_list_node));
  794.  
  795.   /* arg list for the build_function_type call for set_terminate () and
  796.      set_unexpected () */
  797.   temp = tree_cons (NULL_TREE, PFV, void_list_node);
  798.  
  799.   push_lang_context (lang_name_c);
  800.  
  801.   set_terminate_fndecl =
  802.     define_function ("set_terminate",
  803.              build_function_type (PFV, temp),
  804.              NOT_BUILT_IN,
  805.              pushdecl,
  806.              0);
  807.   set_unexpected_fndecl =
  808.     define_function ("set_unexpected",
  809.              build_function_type (PFV, temp),
  810.              NOT_BUILT_IN,
  811.              pushdecl,
  812.              0);
  813.  
  814.   unexpected_fndecl =
  815.     define_function ("unexpected",
  816.              build_function_type (void_type_node, void_list_node),
  817.              NOT_BUILT_IN,
  818.              pushdecl,
  819.              0);
  820.   terminate_fndecl =
  821.     define_function ("terminate",
  822.              build_function_type (void_type_node, void_list_node),
  823.              NOT_BUILT_IN,
  824.              pushdecl,
  825.              0);
  826.   catch_match_fndecl =
  827.     define_function ("__throw_type_match",
  828.              build_function_type (integer_type_node,
  829.                       tree_cons (NULL_TREE, string_type_node, tree_cons (NULL_TREE, ptr_type_node, void_list_node))),
  830.              NOT_BUILT_IN,
  831.              pushdecl,
  832.              0);
  833.   find_first_exception_match_fndecl =
  834.     define_function ("__find_first_exception_table_match",
  835.              build_function_type (ptr_type_node,
  836.                       tree_cons (NULL_TREE, ptr_type_node,
  837.                              void_list_node)),
  838.              NOT_BUILT_IN,
  839.              pushdecl,
  840.              0);
  841.   unwind_fndecl =
  842.     define_function ("__unwind_function",
  843.              build_function_type (void_type_node,
  844.                       tree_cons (NULL_TREE, ptr_type_node, void_list_node)),
  845.              NOT_BUILT_IN,
  846.              pushdecl,
  847.              0);
  848.  
  849.   Unexpected = default_conversion (unexpected_fndecl);
  850.   Terminate = default_conversion (terminate_fndecl);
  851.   SetTerminate = default_conversion (set_terminate_fndecl);
  852.   SetUnexpected = default_conversion (set_unexpected_fndecl);
  853.   CatchMatch = default_conversion (catch_match_fndecl);
  854.   FirstExceptionMatch = default_conversion (find_first_exception_match_fndecl);
  855.   Unwind = default_conversion (unwind_fndecl);
  856.   BuiltinReturnAddress = default_conversion (builtin_return_address_fndecl);
  857.  
  858.   TerminateFunctionCall = build_function_call (Terminate, NULL_TREE);
  859.  
  860.   pop_lang_context ();
  861.   throw_label = gen_label_rtx ();
  862.   saved_pc = gen_rtx (REG, Pmode, 16);
  863.   saved_throw_type = gen_rtx (REG, Pmode, 17);
  864.   saved_throw_value = gen_rtx (REG, Pmode, 18);
  865.  
  866.   new_eh_queue (&ehqueue);
  867.   new_eh_queue (&eh_table_output_queue);
  868.   new_eh_stack (&ehstack);
  869.   new_except_stack (&exceptstack);
  870. }
  871.  
  872. /* call this to begin a block of unwind protection (ie: when an object is
  873.    constructed) */
  874. void
  875. start_protect ()
  876. {
  877.   if (doing_eh (0))
  878.     {
  879.       emit_label (push_eh_entry (&ehstack));
  880.     }
  881. }
  882.    
  883. /* call this to end a block of unwind protection.  the finalization tree is
  884.    the finalization which needs to be run in order to cleanly unwind through
  885.    this level of protection. (ie: call this when a scope is exited)*/
  886. void
  887. end_protect (finalization)
  888.      tree finalization;
  889. {
  890.   struct ehEntry *entry = pop_eh_entry (&ehstack);
  891.  
  892.   if (! doing_eh (0))
  893.     return;
  894.  
  895.   emit_label (entry->end_label);
  896.  
  897.   entry->finalization = finalization;
  898.  
  899.   enqueue_eh_entry (&ehqueue, entry);
  900. }
  901.  
  902. /* call this on start of a try block. */
  903. void
  904. expand_start_try_stmts ()
  905. {
  906.   if (doing_eh (1))
  907.     {
  908.       start_protect ();
  909.     }
  910. }
  911.  
  912. void
  913. expand_end_try_stmts ()
  914. {
  915.   end_protect (integer_zero_node);
  916. }
  917.  
  918. struct insn_save_node {
  919.     rtx last;
  920.     struct insn_save_node *chain;
  921.  };
  922.  
  923. static struct insn_save_node *InsnSave = NULL;
  924.  
  925.  
  926. /* Used to keep track of where the catch blocks start.  */
  927. static void
  928. push_last_insn ()
  929. {
  930.   struct insn_save_node *newnode = (struct insn_save_node*)
  931.     xmalloc (sizeof (struct insn_save_node));
  932.  
  933.   newnode->last = get_last_insn ();
  934.   newnode->chain = InsnSave;
  935.   InsnSave = newnode;
  936. }
  937.  
  938. /* Use to keep track of where the catch blocks start.  */
  939. static rtx
  940. pop_last_insn ()
  941. {
  942.   struct insn_save_node *tempnode;
  943.   rtx temprtx;
  944.  
  945.   if (!InsnSave) return NULL_RTX;
  946.  
  947.   tempnode = InsnSave;
  948.   temprtx = tempnode->last;
  949.   InsnSave = InsnSave->chain;
  950.  
  951.   free (tempnode);
  952.  
  953.   return temprtx;
  954. }
  955.  
  956. /* call this to start processing of all the catch blocks. */
  957. void
  958. expand_start_all_catch ()
  959. {
  960.   struct ehEntry *entry;
  961.   rtx label;
  962.  
  963.   if (! doing_eh (1))
  964.     return;
  965.  
  966.   emit_line_note (input_filename, lineno);
  967.   label = gen_label_rtx ();
  968.   /* The label for the exception handling block we will save.  */
  969.   emit_label (label);
  970.   
  971.   push_label_entry (&caught_return_label_stack, label);
  972.  
  973.   /* Remember where we started. */
  974.   push_last_insn ();
  975.  
  976.   emit_insn (gen_nop ());
  977.  
  978.   /* Will this help us not stomp on it? */
  979.   emit_insn (gen_rtx (USE, VOIDmode, saved_throw_type));
  980.   emit_insn (gen_rtx (USE, VOIDmode, saved_throw_value));
  981.  
  982.   while (1)
  983.     {
  984.       entry = dequeue_eh_entry (&ehqueue);
  985.       emit_label (entry->exception_handler_label);
  986.  
  987.       expand_expr (entry->finalization, const0_rtx, VOIDmode, 0);
  988.  
  989.       /* When we get down to the matching entry, stop.  */
  990.       if (entry->finalization == integer_zero_node)
  991.     break;
  992.  
  993.       free (entry);
  994.     }
  995.  
  996.   /* This goes when the below moves out of our way.  */
  997. #if 1
  998.   label = gen_label_rtx ();
  999.   emit_jump (label);
  1000. #endif
  1001.   
  1002.   /* All this should be out of line, and saved back in the exception handler
  1003.      block area.  */
  1004. #if 1
  1005.   entry->start_label = entry->exception_handler_label;
  1006.   /* These are saved for the exception table.  */
  1007.   push_rtl_perm ();
  1008.   entry->end_label = gen_label_rtx ();
  1009.   entry->exception_handler_label = gen_label_rtx ();
  1010.   entry->finalization = TerminateFunctionCall;
  1011.   pop_rtl_from_perm ();
  1012.   emit_label (entry->end_label);
  1013.  
  1014.   enqueue_eh_entry (&eh_table_output_queue, copy_eh_entry (entry));
  1015.  
  1016.   /* After running the finalization, continue on out to the next
  1017.      cleanup, if we have nothing better to do.  */
  1018.   emit_move_insn (saved_pc, gen_rtx (LABEL_REF, Pmode, entry->end_label));
  1019.   /* Will this help us not stomp on it? */
  1020.   emit_insn (gen_rtx (USE, VOIDmode, saved_throw_type));
  1021.   emit_insn (gen_rtx (USE, VOIDmode, saved_throw_value));
  1022.   emit_jump (throw_label);
  1023.   emit_label (entry->exception_handler_label);
  1024.   expand_expr (entry->finalization, const0_rtx, VOIDmode, 0);
  1025.   emit_barrier ();
  1026. #endif
  1027.   emit_label (label);
  1028. }
  1029.  
  1030. /* call this to end processing of all the catch blocks. */
  1031. void
  1032. expand_end_all_catch ()
  1033. {
  1034.   rtx catchstart, catchend, last;
  1035.   rtx label;
  1036.  
  1037.   if (! doing_eh (1))
  1038.     return;
  1039.  
  1040.   /* Find the start of the catch block.  */
  1041.   last = pop_last_insn ();
  1042.   catchstart = NEXT_INSN (last);
  1043.   catchend = get_last_insn ();
  1044.  
  1045.   NEXT_INSN (last) = 0;
  1046.   set_last_insn (last);
  1047.  
  1048.   /* this level of catch blocks is done, so set up the successful catch jump
  1049.      label for the next layer of catch blocks. */
  1050.   pop_label_entry (&caught_return_label_stack);
  1051.  
  1052.   push_except_stmts (&exceptstack, catchstart, catchend);
  1053.   
  1054.   /* Here we fall through into the continuation code.  */
  1055. }
  1056.  
  1057.  
  1058. /* this is called from expand_exception_blocks () to expand the toplevel
  1059.    finalizations for a function. */
  1060. void
  1061. expand_leftover_cleanups ()
  1062. {
  1063.   struct ehEntry *entry;
  1064.   rtx first_label = NULL_RTX;
  1065.  
  1066.   if (! doing_eh (0))
  1067.     return;
  1068.  
  1069.   /* Will this help us not stomp on it? */
  1070.   emit_insn (gen_rtx (USE, VOIDmode, saved_throw_type));
  1071.   emit_insn (gen_rtx (USE, VOIDmode, saved_throw_value));
  1072.  
  1073.   while ((entry = dequeue_eh_entry (&ehqueue)) != 0)
  1074.     {
  1075.       if (! first_label)
  1076.     first_label = entry->exception_handler_label;
  1077.       emit_label (entry->exception_handler_label);
  1078.  
  1079.       expand_expr (entry->finalization, const0_rtx, VOIDmode, 0);
  1080.  
  1081.       /* leftover try block, opps.  */
  1082.       if (entry->finalization == integer_zero_node)
  1083.     abort ();
  1084.  
  1085.       free (entry);
  1086.     }
  1087.   if (first_label)
  1088.     {
  1089.       rtx label;
  1090.       struct ehEntry entry;
  1091.       /* These are saved for the exception table.  */
  1092.       push_rtl_perm ();
  1093.       label = gen_label_rtx ();
  1094.       entry.start_label = first_label;
  1095.       entry.end_label = label;
  1096.       entry.exception_handler_label = gen_label_rtx ();
  1097.       entry.finalization = TerminateFunctionCall;
  1098.       pop_rtl_from_perm ();
  1099.       emit_label (label);
  1100.  
  1101.       enqueue_eh_entry (&eh_table_output_queue, copy_eh_entry (&entry));
  1102.  
  1103.       /* After running the finalization, continue on out to the next
  1104.      cleanup, if we have nothing better to do.  */
  1105.       emit_move_insn (saved_pc, gen_rtx (LABEL_REF, Pmode, entry.end_label));
  1106.       /* Will this help us not stomp on it? */
  1107.       emit_insn (gen_rtx (USE, VOIDmode, saved_throw_type));
  1108.       emit_insn (gen_rtx (USE, VOIDmode, saved_throw_value));
  1109.       emit_jump (throw_label);
  1110.       emit_label (entry.exception_handler_label);
  1111.       expand_expr (entry.finalization, const0_rtx, VOIDmode, 0);
  1112.       emit_barrier ();
  1113.     }
  1114. }
  1115.  
  1116. /* call this to start a catch block. Typename is the typename, and identifier
  1117.    is the variable to place the object in or NULL if the variable doesn't
  1118.    matter.  If typename is NULL, that means its a "catch (...)" or catch
  1119.    everything.  In that case we don't need to do any type checking.
  1120.    (ie: it ends up as the "else" clause rather than an "else if" clause) */
  1121. void
  1122. expand_start_catch_block (declspecs, declarator)
  1123.      tree declspecs, declarator;
  1124. {
  1125.   rtx false_label_rtx;
  1126.   rtx protect_label_rtx;
  1127.   tree type;
  1128.   tree decl;
  1129.   tree init;
  1130.  
  1131.   if (! doing_eh (1))
  1132.     return;
  1133.  
  1134.   /* Create a binding level for the parm.  */
  1135.   expand_start_bindings (0);
  1136.  
  1137.   if (declspecs)
  1138.     {
  1139.       tree init_type;
  1140.       decl = grokdeclarator (declarator, declspecs, NORMAL, 1, NULL_TREE);
  1141.  
  1142.       /* Figure out the type that the initializer is. */
  1143.       init_type = TREE_TYPE (decl);
  1144.       if (TREE_CODE (init_type) != REFERENCE_TYPE)
  1145.     init_type = build_reference_type (init_type);
  1146.  
  1147.       init = convert_from_reference (save_expr (make_tree (init_type, saved_throw_value)));
  1148.       
  1149.       /* Do we need the below two lines? */
  1150.       /* Let `finish_decl' know that this initializer is ok.  */
  1151.       DECL_INITIAL (decl) = init;
  1152.       /* This needs to be preallocated under the try block,
  1153.      in a union of all catch variables. */
  1154.       pushdecl (decl);
  1155.       type = TREE_TYPE (decl);
  1156.  
  1157.       /* peel back references, so they match. */
  1158.       if (TREE_CODE (type) == REFERENCE_TYPE)
  1159.     type = TREE_TYPE (type);
  1160.     }
  1161.   else
  1162.     type = NULL_TREE;
  1163.  
  1164.   false_label_rtx = gen_label_rtx ();
  1165.   push_label_entry (&false_label_stack, false_label_rtx);
  1166.  
  1167.   /* This is saved for the exception table.  */
  1168.   push_rtl_perm ();
  1169.   protect_label_rtx = gen_label_rtx ();
  1170.   pop_rtl_from_perm ();
  1171.   push_label_entry (&false_label_stack, protect_label_rtx);
  1172.  
  1173.   if (type)
  1174.     {
  1175.       tree params;
  1176.       char *typestring;
  1177.       rtx call_rtx, return_value_rtx;
  1178.       tree catch_match_fcall;
  1179.       tree catchmatch_arg, argval;
  1180.  
  1181.       typestring = build_overload_name (type, 1, 1);
  1182.  
  1183.       params = tree_cons (NULL_TREE,
  1184.              combine_strings (build_string (strlen (typestring)+1, typestring)),
  1185.              tree_cons (NULL_TREE,
  1186.                     make_tree (ptr_type_node, saved_throw_type),
  1187.                     NULL_TREE));
  1188.       catch_match_fcall = build_function_call (CatchMatch, params);
  1189.       call_rtx = expand_call (catch_match_fcall, NULL_RTX, 0);
  1190.  
  1191.       return_value_rtx =
  1192.     hard_function_value (integer_type_node, catch_match_fcall);
  1193.  
  1194.       /* did the throw type match function return TRUE? */
  1195.       emit_cmp_insn (return_value_rtx, const0_rtx, NE, NULL_RTX,
  1196.             GET_MODE (return_value_rtx), 0, 0);
  1197.  
  1198.       /* if it returned FALSE, jump over the catch block, else fall into it */
  1199.       emit_jump_insn (gen_bne (false_label_rtx));
  1200.       finish_decl (decl, init, NULL_TREE, 0);
  1201.     }
  1202.   else
  1203.     {
  1204.       /* Fall into the catch all section. */
  1205.     }
  1206.  
  1207.   /* This is the starting of something to protect.  */
  1208.   emit_label (protect_label_rtx);
  1209.  
  1210.   emit_line_note (input_filename, lineno);
  1211. }
  1212.  
  1213.  
  1214. /* Call this to end a catch block.  Its responsible for emitting the
  1215.    code to handle jumping back to the correct place, and for emitting
  1216.    the label to jump to if this catch block didn't match.  */
  1217. void expand_end_catch_block ()
  1218. {
  1219.   if (doing_eh (1))
  1220.     {
  1221.       rtx start_protect_label_rtx;
  1222.       rtx end_protect_label_rtx;
  1223.       tree decls;
  1224.       struct ehEntry entry;
  1225.  
  1226.       /* label we jump to if we caught the exception */
  1227.       emit_jump (top_label_entry (&caught_return_label_stack));
  1228.  
  1229.       /* Code to throw out to outer context, if we get an throw from within
  1230.      our catch handler. */
  1231.       /* These are saved for the exception table.  */
  1232.       push_rtl_perm ();
  1233.       entry.exception_handler_label = gen_label_rtx ();
  1234.       pop_rtl_from_perm ();
  1235.       emit_label (entry.exception_handler_label);
  1236.       emit_move_insn (saved_pc, gen_rtx (LABEL_REF,
  1237.                      Pmode,
  1238.                      top_label_entry (&caught_return_label_stack)));
  1239.       emit_jump (throw_label);
  1240.       /* No associated finalization.  */
  1241.       entry.finalization = NULL_TREE;
  1242.  
  1243.       /* Because we are reordered out of line, we have to protect this. */
  1244.       /* label for the start of the protection region.  */
  1245.       start_protect_label_rtx = pop_label_entry (&false_label_stack);
  1246.  
  1247.       /* Cleanup the EH paramater.  */
  1248.       expand_end_bindings (decls = getdecls (), decls != NULL_TREE, 0);
  1249.       
  1250.       /* label we emit to jump to if this catch block didn't match. */
  1251.       emit_label (end_protect_label_rtx = pop_label_entry (&false_label_stack));
  1252.  
  1253.       /* Because we are reordered out of line, we have to protect this. */
  1254.       entry.start_label = start_protect_label_rtx;
  1255.       entry.end_label = end_protect_label_rtx;
  1256.  
  1257.       /* These set up a call to throw the caught exception into the outer
  1258.        context.  */
  1259.       enqueue_eh_entry (&eh_table_output_queue, copy_eh_entry (&entry));
  1260.     }
  1261. }
  1262.  
  1263. /* cheesyness to save some typing. returns the return value rtx */
  1264. rtx
  1265. do_function_call (func, params, return_type)
  1266.      tree func, params, return_type;
  1267. {
  1268.   tree func_call;
  1269.   func_call = build_function_call (func, params);
  1270.   expand_call (func_call, NULL_RTX, 0);
  1271.   if (return_type != NULL_TREE)
  1272.     return hard_function_value (return_type, func_call);
  1273.   return NULL_RTX;
  1274. }
  1275.  
  1276.  
  1277. /* is called from expand_excpetion_blocks () to generate the code in a function
  1278.    to "throw" if anything in the function needs to preform a throw.
  1279.  
  1280.    expands "throw" as the following psuedo code:
  1281.  
  1282.     throw:
  1283.         eh = find_first_exception_match (saved_pc);
  1284.         if (!eh) goto gotta_rethrow_it;
  1285.         goto eh;
  1286.  
  1287.     gotta_rethrow_it:
  1288.         saved_pc = __builtin_return_address (0);
  1289.         pop_to_previous_level ();
  1290.         goto throw;
  1291.  
  1292.  */
  1293. static void
  1294. expand_builtin_throw ()
  1295. {
  1296.   tree fcall;
  1297.   tree params;
  1298.   rtx return_val_rtx;
  1299.   rtx gotta_rethrow_it = gen_label_rtx ();
  1300.   rtx gotta_call_terminate = gen_label_rtx ();
  1301.   rtx unwind_and_throw = gen_label_rtx ();
  1302.   rtx goto_unwind_and_throw = gen_label_rtx ();
  1303.  
  1304.   emit_label (throw_label);
  1305.  
  1306.   /* search for an exception handler for the saved_pc */
  1307.   return_val_rtx = do_function_call (FirstExceptionMatch,
  1308.                      tree_cons (NULL_TREE, make_tree (ptr_type_node, saved_pc), NULL_TREE),
  1309.                      ptr_type_node);
  1310.  
  1311.   /* did we find one? */
  1312.   emit_cmp_insn (return_val_rtx, const0_rtx, EQ, NULL_RTX,
  1313.          GET_MODE (return_val_rtx), 0, 0);
  1314.  
  1315.   /* if not, jump to gotta_rethrow_it */
  1316.   emit_jump_insn (gen_beq (gotta_rethrow_it));
  1317.  
  1318.   /* we found it, so jump to it */
  1319.   emit_indirect_jump (return_val_rtx);
  1320.  
  1321.   /* code to deal with unwinding and looking for it again */
  1322.   emit_label (gotta_rethrow_it);
  1323.  
  1324.   /* call to  __builtin_return_address () */
  1325.   params=tree_cons (NULL_TREE, integer_zero_node, NULL_TREE);
  1326.   fcall = build_function_call (BuiltinReturnAddress, params);
  1327.   return_val_rtx = expand_expr (fcall, NULL_RTX, SImode, 0);
  1328.  
  1329.   /* did __builtin_return_address () return a valid address? */
  1330.   emit_cmp_insn (return_val_rtx, const0_rtx, EQ, NULL_RTX,
  1331.          GET_MODE (return_val_rtx), 0, 0);
  1332.  
  1333.   emit_jump_insn (gen_beq (gotta_call_terminate));
  1334.  
  1335.   /* yes it did */
  1336.   emit_move_insn (saved_pc, return_val_rtx);
  1337.   do_unwind (throw_label);
  1338.   emit_jump (throw_label);
  1339.  
  1340.   /* no it didn't --> therefore we need to call terminate */
  1341.   emit_label (gotta_call_terminate);
  1342.   do_function_call (Terminate, NULL_TREE, NULL_TREE);
  1343. }
  1344.  
  1345.  
  1346. /* This is called to expand all the toplevel exception handling
  1347.    finalization for a function.  It should only be called once per
  1348.    function.  */
  1349. void
  1350. expand_exception_blocks ()
  1351. {
  1352.   rtx catchstart, catchend;
  1353.   rtx last;
  1354.   static rtx funcend;
  1355.  
  1356.   funcend = gen_label_rtx ();
  1357.   emit_jump (funcend);
  1358.   /* expand_null_return (); */
  1359.  
  1360.   while (pop_except_stmts (&exceptstack, &catchstart, &catchend)) {
  1361.     last = get_last_insn ();
  1362.     NEXT_INSN (last) = catchstart;
  1363.     PREV_INSN (catchstart) = last;
  1364.     NEXT_INSN (catchend) = 0;
  1365.     set_last_insn (catchend);
  1366.   }
  1367.  
  1368.   expand_leftover_cleanups ();
  1369.  
  1370.   {
  1371.     static int have_done = 0;
  1372.     if (! have_done && TREE_PUBLIC (current_function_decl)
  1373.     && ! DECL_INLINE (current_function_decl))
  1374.       {
  1375.     have_done = 1;
  1376.     expand_builtin_throw ();
  1377.       }
  1378.   }
  1379.   emit_label (funcend);
  1380. }
  1381.  
  1382.  
  1383. /* call this to expand a throw statement.  This follows the following
  1384.    algorithm:
  1385.  
  1386.     1. Allocate space to save the current PC onto the stack.
  1387.     2. Generate and emit a label and save its address into the
  1388.         newly allocate stack space since we can't save the pc directly.
  1389.     3. If this is the first call to throw in this function:
  1390.         generate a label for the throw block
  1391.     4. jump to the throw block label.  */
  1392. void
  1393. expand_throw (exp)
  1394.      tree exp;
  1395. {
  1396.   rtx label;
  1397.   tree type;
  1398.  
  1399.   if (! doing_eh (1))
  1400.     return;
  1401.  
  1402.   /* This is the label that represents where in the code we were, when
  1403.      we got an exception.  This needs to be updated when we rethrow an
  1404.      exception, so that the matching routine knows to search out.  */
  1405.   label = gen_label_rtx ();
  1406.   emit_label (label);
  1407.   emit_move_insn (saved_pc, gen_rtx (LABEL_REF, Pmode, label));
  1408.  
  1409.   if (exp)
  1410.     {
  1411.       /* throw expression */
  1412.       /* First, decay it. */
  1413.       exp = default_conversion (exp);
  1414.       type = TREE_TYPE (exp);
  1415.  
  1416.       {
  1417.     char *typestring = build_overload_name (type, 1, 1);
  1418.     tree throw_type = build1 (ADDR_EXPR, ptr_type_node, combine_strings (build_string (strlen (typestring)+1, typestring)));
  1419.     rtx throw_type_rtx = expand_expr (throw_type, NULL_RTX, VOIDmode, 0);
  1420.     rtx throw_value_rtx;
  1421.  
  1422.     emit_move_insn (saved_throw_type, throw_type_rtx);
  1423.     exp = convert_to_reference (build_reference_type (build_type_variant (TREE_TYPE (exp), 1, 0)), exp, CONV_STATIC, LOOKUP_COMPLAIN, NULL_TREE);
  1424.     if (exp == error_mark_node)
  1425.       error ("  in thrown expression");
  1426.     throw_value_rtx = expand_expr (build_unary_op (ADDR_EXPR, exp, 0), NULL_RTX, VOIDmode, 0);
  1427.     emit_move_insn (saved_throw_value, throw_value_rtx);
  1428.       }
  1429.     }
  1430.   else
  1431.     {
  1432.       /* rethrow current exception */
  1433.       /* This part is easy, as we dont' have to do anything else.  */
  1434.     }
  1435.  
  1436.   emit_jump (throw_label);
  1437. }
  1438.  
  1439.  
  1440. /* output the exception table */
  1441. void
  1442. build_exception_table ()
  1443. {
  1444.   extern FILE *asm_out_file;
  1445.   struct ehEntry *entry;
  1446.  
  1447.   if (! doing_eh (0))
  1448.     return;
  1449.  
  1450.   exception_section ();
  1451.  
  1452.   /* Beginning marker for table. */
  1453.   fprintf (asm_out_file, "        .global ___EXCEPTION_TABLE__\n");
  1454.   fprintf (asm_out_file, "        .align 4\n");
  1455.   fprintf (asm_out_file, "___EXCEPTION_TABLE__:\n");
  1456.   fprintf (asm_out_file, "        .word   0, 0, 0\n");
  1457.  
  1458.  while (entry = dequeue_eh_entry (&eh_table_output_queue)) {
  1459.      output_exception_table_entry (asm_out_file,
  1460.          entry->start_label, entry->end_label, entry->exception_handler_label);
  1461.   }
  1462.  
  1463.   /* Ending marker for table. */
  1464.   fprintf (asm_out_file, "        .global ___EXCEPTION_END__\n");
  1465.   fprintf (asm_out_file, "___EXCEPTION_END__:\n");
  1466.   fprintf (asm_out_file, "        .word   -1, -1, -1\n");
  1467. }
  1468.  
  1469. /* end of: my-cp-except.c */
  1470. #endif
  1471.  
  1472.  
  1473. /* Build a throw expression.  */
  1474. tree
  1475. build_throw (e)
  1476.      tree e;
  1477. {
  1478.   e = build1 (THROW_EXPR, void_type_node, e);
  1479.   TREE_SIDE_EFFECTS (e) = 1;
  1480.   return e;
  1481. }
  1482.